<style scoped>
  .weui-form-preview__hd .weui-form-preview__value {
    font-size: 1.2em;
    padding-right: 20px;
  }
  .page-infinite-wrapper {
    background: #e3e3e3;
  }
  .sellform >>> .weui-cells {
    margin-top: 0;
    background: #e3e3e3;
  }
  .sellform >>> .weui-loadmore__tips {
    background: #e3e3e3;;
  }
  .sellform >>> .weui-loadmore {
    margin-bottom: 0;
  }
  .weui-form-preview {
    margin: 5px;
    border-radius: 5px;
  }

  .rj-cell-child {
    display: inline-block;
    font-size: 0;
    padding: 5px 15px;
    text-align: left;
  }
  .rj-cell-child.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell-child.minpad{
    padding-right: 10px !important;
  }
  .rj-cell-child label {
    color: #333;
    font-size: 14px;
  }
  .rj-cell-child span {
    padding-left: 20px;
    font-size: 14px;
    color: #333;
  }
  .rj-cell-child .tip {
    font-size: 16px;
    color: #333;
  }
  .rj-cell-child i {
    font-size: 18px;
    font-style: normal;
    color: #333;
  }
  .rj-cell-child-tit {
    background: #eee;
    font-size: 14px;
    padding: 5px 15px;
    border-bottom: 1px solid #ddd;
  }
  .weui-form-preview__ft .weui-form-preview {
    background: #f5f5ef;
  }
  .weui-form-preview__ft {
    line-height: normal;
  }



  .weui_tab_bd {
    background: #eee;
  }
  .weui-form-preview {
    background-color: #fff;
    margin: 5px;
    border-radius: 5px;
  }
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui_navbar {
    white-space: nowrap;
    overflow: scroll;
    display: block !important;
    border-bottom: 1px solid #BCBAB6;
    font-size: 0;
    /*background: #fafafa;*/
  }
  .weui_navbar_item {
    font-size: 14px;
    display: inline-block;
    /*width: 25%;*/
    padding: 13px 0;
    background: #fff;
    position: relative;
    color: #888;
  }
  .weui_navbar_item.weui_bar_item_on {
    background-color: #f5f5f5;
    color: #00913a;
  }
  .weui_navbar:after {
    border-bottom: 0;
  }
  .weui_navbar_item:after {
    content: " ";
    position: absolute;
    right: 0;
    top: 0;
    width: 1px;
    height: 100%;
    border-right: 1px solid #cccccc;
    color: #cccccc;
    -webkit-transform-origin: 100% 0;
    transform-origin: 100% 0;
    -webkit-transform: scaleX(0.5);
    transform: scaleX(0.5);
    font-size: 10px;
  }
  .weui-panel__bd {
    text-align: left;
  }
  .hoverbar {
    background: #03A9F4;
    position: fixed;
    bottom: 100px;
    right: 20px;
    height: 50px;
    line-height: 50px;
    width: 50px;
    z-index: 100;
    font-size: 30px;
    border-radius: 50%;
    box-shadow: 0px 2px 3px rgba(100, 100, 100, 0.5);
    color: #fff;
  }
  .flowbtn {
    position: absolute;
    top: 5px;
    right: 5px;
    z-index: 1000;
  }
  .rj-edit-em {
    font-size: 18px;
    padding-right: 8px;
    font-style: normal;
  }
  .rj-edit-em.red {
    color: red;
  }
</style>

<template>
  <div class="freightlist">
    <div class="hoverbar" v-if="addPerm"  @click="addItem">+</div>
    <SearchBar @doSearch="doSearch" />
    <div class="weui_tab">
      <div class="weui_navbar order-tabhd">
        <a class="weui_navbar_item" v-for="(item, index) in tabTitles" :key="item.guid" :style="'width:' + tabWidth" :class="{ weui_bar_item_on: postParams.status == item.stats }" href="javaScript:;" @click="switchStatus(item.stats)">{{item.name}}</a>
      </div>
      <div class="weui_tab_bd" style="background-color: #fff">
        <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }" >
          <mescroll-vue ref="mescroll" id="wrapper" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
            <div class="weui-form-preview" v-for="(item, index) in data" :key="item.guid" :ref="item.guid">
              <div class="flowbtn" @click="openLogs(item.guid, item.strguid, item.departid)"><img src="@/assets/icon-history.png" style="width: 16px" /></div>
              <div class="weui-form-preview__hd" @click="viewItem(item.guid)">
                <label class="weui-form-preview__label">编号</label>
                <em class="weui-form-preview__value">{{item.code}}</em>
              </div>
              <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">{{item.plantor}}</label>
                  <span class="weui-form-preview__value">{{item.arealand}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">承运人</label>
                  <span class="weui-form-preview__value"><em class="rj-edit-em">{{item.carcode}}</em>{{item.drivername}}&nbsp;&nbsp;{{item.driverphone}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">货品信息</label>
                  <span class="weui-form-preview__value">
                    {{item.variety}} {{item.setlwgtype == 1 ? item.weight : item.hweight}}吨
                    <span v-if="item.qamaintype != 2 || (item.qamaintype == 2 && item.status > 20)">{{item.price}}元/吨</span>
                  </span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">规格</label>
                  <span class="weui-form-preview__value">{{item.specification}}</span>
                </div>
                <div class="weui-form-preview__item" v-if="item.harvest == 2">
                  <label class="weui-form-preview__label">库房</label>
                  <span class="weui-form-preview__value">{{item.storeroom}}  {{item.storehouse}}</span>
                </div>
                <div class="weui-form-preview__item" v-else>
                  <label class="weui-form-preview__label">目的地</label>
                  <span class="weui-form-preview__value">{{item.provence}}{{item.city}}{{item.country}}{{item.street}}</span>
                </div>
                <template  v-show="postParams.status == 4 || postParams.status == 5">
                  <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">结算金额</label>
                    <span class="weui-form-preview__value">{{item.settlement}}</span>
                  </div>
                </template>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">付款方式</label>
                  <span class="weui-form-preview__value">{{item.setlwgtype == 1 ? '装车付' : '验收付'}}</span>
                </div>
<!--                <div class="weui-form-preview__item">-->
<!--                  <label class="weui-form-preview__label">质检方式</label>-->
<!--                  <span class="weui-form-preview__value">{{item.qatype == 1 ? '库房质检' : '库房质检'}}</span>-->
<!--                </div>-->
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">质检标准</label>
                  <span class="weui-form-preview__value">{{item.qaname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">备注</label>
                  <span class="weui-form-preview__value">{{item.remark}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">装车时间</label>
                  <span class="weui-form-preview__value">{{item.createtime}}</span>
                </div>
                <div class="weui-form-preview__item" v-show="item.address">
                  <img src="../../../../static/image/icon-location.png" width="15px" height="15px" />
                  <span style="color:forestgreen;font-size: 12px;">{{item.address}}</span>
                </div>
                <div class="weui-form-preview__item" v-show="item.distance">
                  <img src="../../../../static/image/icon-distance.png" width="15px" height="15px" />
                  <span style="color:forestgreen;font-size: 12px;">{{item.distance}} Km</span>
                </div>
                <div class="weui-form-preview__item" v-show="item.unloadtime">
                  <label class="weui-form-preview__label">卸货时间</label>
                  <span class="weui-form-preview__value">{{item.unloadtime}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">支付状态</label>
                  <span class="weui-form-preview__value">{{item.paystatusname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">状态</label>
                  <span class="weui-form-preview__value">{{item.statusname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">负责人</label>
                  <span class="weui-form-preview__value">{{item.operator}} - {{item.department}}</span>
                </div>
              </div>

              <div class="weui-form-preview__ft" style="line-height: 50px;" >
                <template  v-for="(node, index) in item.nodes">
                  <a class="weui-form-preview__btn weui-form-preview__btn_primary" :key="node.guid" href="javascript:" v-if="filterPerm(node.permission, item.strguid, item.departid, item.operatorid) && !(node.operate == 'weigh' && item.setlwgtype == 2 && item.paystatus >= 3)" @click="doAct(node.operate, item.guid, node.guid, node.name, node.annex, node.refuseflag)" >{{node.name}}</a>
                </template>
<!--                <a class="weui-form-preview__btn weui-form-preview__btn_primary" v-if="postParams.status == 35 && item.paystatus < 3 && filterPerm(backwhnode.permission, item.strguid, item.departid, item.operatorid)" href="javascript:" @click="doAct(backwhnode.operate, item.guid, backwhnode.guid, backwhnode.name, backwhnode.annex, backwhnode.refuseflag)" >{{backwhnode.name}}</a>-->
              </div>
            </div>
          </mescroll-vue>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/babel">
  import MescrollVue from 'mescroll.js/mescroll.vue'
  import SearchBar from '../../common/SearchBar'
  export default {
    components: {SearchBar, MescrollVue},
    data() {
      return {
        flowid: 24,
        userid: localStorage.userid,
        permission: {},
        permissions: [],
        isShow:true,
        postParams: {
          status: '00'
        },
        mescroll: null, // mescroll实例对象
        mescrollDown: {},
        mescrollUp: { // 上拉加载的配置.
          callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
          page: {
            num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
            size: 5 //每页数据条数,默认10
          },
          htmlNodata: '<p class="upwarp-nodata">-- 我是有底线的 --</p>',
          noMoreSize: 1,
          empty: {
            warpId: "wrapper", //父布局的id (1.3.5版本支持传入dom元素)
            tip: "暂无相关数据~" //提示
          }
        },
        wrapperHeight: 600,
        data: [],
        tabTitles: [],
        tabWidth: '25%',
        addPerm: '',
        backwhnode: ''
      }
    },
    created() {
      //列表页头部不允许滑动
      document.getElementsByTagName("body")[0].className="bg";
    },
    async mounted() {
      this.getLabels();
      if (this.$route.query.code != undefined) {
        this.postParams.search = this.$route.query.code;
      }
      if (this.$route.query.status != undefined) {
        this.postParams.status = this.$route.query.status;
      }
      // this.wrapperHeight = document.documentElement.clientHeight - this.$refs.wrapper.getBoundingClientRect().top - 40;

      //获取权限
      await this.getPermission();
      this.getAddPermis();
      this.getBackwhinNode();
    },
    activated () {
      //列表页头部不允许滑动
      document.getElementsByTagName("body")[0].className="bg";

      //判断是否是返回上一页
      if (this.$store.state.list.reffer != this.$options.name) return;
      //获取上一次访问列表数据
      this.mescrollUp.page.num = 0;
      this.mescrollUp.page.size = this.$store.state.list.pageNo * this.mescrollUp.page.size;
      this.getPageData();
      //定位
      this.$nextTick(function(){
        let position = this.$store.state.list.postion //返回页面取出来
        $('#wrapper').scrollTop(position);
        let status = this.$store.state.list.status;
        this.postParams.status = status;
      })
    },
    methods: {
      getLabels: function() {
        this.axios({
          method: 'get',
          params:{flowid: this.flowid},
          url: '/produce/Label!getLabels.action',
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.$nextTick(() => {
              this.tabTitles = response.data.labels;
              //tab最大宽度20%，默认选中第一个tab页
              this.tabWidth = response.data.labels.length < 6 ? (100 / response.data.labels.length) + '%' : '20%';
              if (this.$route.query.label != undefined) {
                this.postParams.status = this.tabTitles[this.currTab].stats;
                this.getPageData();
              } else if (this.postParams.status != this.tabTitles[0].stats) {
                this.postParams.status = this.tabTitles[0].stats;
                this.getPageData();
              } else {
                this.postParams.status = this.tabTitles[0].stats;
              }
            })
          } else {
            this.toastError("系统错误");
          }
        })
      },
      getAddPermis: function() {
        this.axios({
          method: 'get',
          params:{flowid: 24},
          url: '/sales/Flowtempnode!getLaunchPerm.action',
        }).then((response) => {
          if (response.data.msg == 'success') {
            console.log('this.permissions：', this.permissions)
            this.addPerm = this.permissions.some(item => {
              return item.permission == response.data.permission;
            })
            console.log('this.addPerm:', this.addPerm)
          }

        })
      },
      getPermission: async function() {
        await this.axios({
          method: 'get',
          params:{opttype: 26},
          url: '/produce/OperatorPermission!getOperPerm.action',
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.$nextTick(() => {
              this.permissions = response.data.permission;
            })
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getBackwhinNode: function() {
        this.axios({
          method: 'get',
          params:{flowid: 24, nodetype: 999},
          url: '/sales/Flowtempnode!getNode.action',
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.backwhnode = response.data.node;
          }
        })
      },
      switchStatus: function (sta) {
        this.postParams.status = sta;
        this.getPageData();
      },
      mescrollInit (mescroll) {
        this.mescroll = mescroll  // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
      },
      upCallback (page, mescroll) {
        //alert(this.postParams.code)
        this.postParams.pageNo = page.num;
        this.postParams.pageSize = page.size;
        this.postParams.functiontype = 26;
        // 联网请求
        this.axios({
          method: 'get',
          url: '/harvest/Freightorder!page.action',
          params: this.postParams
        }).then((response) => {
          if (response.data.msg == 'success') {
            let res = response.data != undefined ? response.data : {pagelist: {rows: []}};

            // 如果是第一页需手动置空列表
            if (page.num === 1 && this.data.length != 0) this.data = [];
            // 把请求到的数据添加到列表
            this.data = this.data.concat(res.pagelist.rows);
            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(res.pagelist.rows.length)
            })
            //将获取上一访问列表临时页码恢复正常
            if (this.mescrollUp.page.size > 5) {
              this.mescrollUp.page.num = this.$store.state.list.pageNo;
              this.mescrollUp.page.size = 5;
            }
          } else {
            this.toastError("系统错误");
          }

        }).catch((e) => {
          // 联网失败的回调,隐藏下拉刷新和上拉加载的状态;
          mescroll.endErr()
        })
      },
      filterPerm: function(perm, roomid, departid, operatorid) {
        let status = false;
        if (perm == 0) {
          console.log('this.userid：', this.userid)
          console.log('operatorid：', operatorid)
          console.log('this.userid == operatorid：', this.userid == operatorid)
          status = this.userid == operatorid;
          console.log('first status:', status);
        } else {
          let permissions = this.permissions;
          if (permissions.length == 0) return;
          for (let i=0; i<permissions.length; i++) {
            if (perm == permissions[i].permission) {
              if (permissions[i].permss.scopetype == 7) {
                if (permissions[i].srguid == roomid) {
                  status = true;
                } else {
                  status = status == true ? true :false;
                }
              } else if (permissions[i].permss.scopetype == 25) {
                if (permissions[i].departid == departid) {
                  status = true;
                } else {
                  status = status == true ? true :false;
                }
              } else if (permissions[i].permss.scopetype == 3) {
                if (permissions[i].srguid == roomid && permissions[i].departid == departid) {
                  status = true;
                } else {
                  status = status == true ? true :false;
                }
              } else if (permissions[i].permss.scopetype == 4) {
                status = true;
              } else {
                status = true;
              }
              //return status;

            }
          }

        }
        console.log('last status:', status);
        return status;
      },
      getPageData: function () {
        $('#wrapper').scrollTop(0);
        this.data = [];
        this.mescroll.resetUpScroll();
      },
      doSearch: function(keywords) {
        this.postParams.search = keywords;
        this.getPageData();
      },
      viewItem: function(guid) {
        this.$router.push({name: 'ViewForder', query: {id: guid, fromurl: encodeURIComponent(location.href)}});
        // window.location.href = '/harvest/Freightorder!detail.action?orderType=1&door=newlist&guid='+ guid;
      },
      doAct: async function (name, guid, nodeid, actname, annex, refuseflag) {
        let that = this;
        let acts = this.$store.state.acts;
        if (name == 'quality') {
          if (annex) {
            window.location.href = '/trace/Qaform!index.action?orderguid='+guid+'&orderType=1'+'&nodeid='+nodeid+'&annex='+annex;
          } else {
            window.location.href = '/trace/Qaform!index.action?orderguid='+guid+'&orderType=1'+'&nodeid='+nodeid;
          }
        } else if (name == 'discharge') {
          $.confirm("扫码卸货确认", function (text) {
            that.axios({
              method: 'post',
              url: '/harvest/Freightorder!discharge.action',
              params: {guid: guid}
            }).then((response) => {
              if (response.data.status == 'success') {
                that.toastSuccess('操作成功！');
                that.getPageData();
              } else {
                that.toastError("系统错误");
              }

            })
          }, function () {
            //点击取消后的回调函数
          });
        } else {
          that.$router.push({name: acts[name], query: {guid:guid, nodeid: nodeid, title: actname, annex: annex, refuseflag: refuseflag}});
        }
      },
      addItem: function() {
        this.$router.push({name: 'AddFreightOrder'});
      },
      openLogs: function(id, roomid, departid) {
        this.$router.push({name: 'SalesOperationLogs3', query: {orderid: id, srguid: roomid, departid: departid}});
      }
    },
    beforeRouteLeave(to, from, next){
      //取消列表页头部不允许滑动
      document.body.removeAttribute("class","bg");

      let position = $('#wrapper').scrollTop();
      let status = this.postParams.status;
      this.$store.commit('setPosition', position) //离开路由时把位置存起来
      this.$store.commit('setStatus', status)
      this.$store.commit('setPageNo', this.mescrollUp.page.num);
      this.$store.commit('setReffer', this.$options.name);
      next()
    },



  };
</script>
